#include "stdafx.h"

#include "cTimeMark.h"

TimeMark::TimeMark() {};

TimeMark::TimeMark(LPCTSTR strTime, BOOL bSeconds/* = FALSE*/)
{
	set(this, strTime, bSeconds);
}

TimeMark::TimeMark(LPCTSTR strDate, LPCTSTR strTime)
{
	set(this, strDate, strTime);
}

void TimeMark::set(TimeMark* tm, LPCTSTR strTime, BOOL bSeconds/* = FALSE*/)
{
	int year, month, day, hour, minute, second;
	year = month = day = hour = minute = second = 0;
	if(bSeconds)
		sscanf(strTime, _T("%d %d %d %d %d %d"), &year, &month, &day, &hour, &minute, &second);
	else
		sscanf(strTime, _T("%d %d %d %d %d"), &year, &month, &day, &hour, &minute);
	tm->bDay = day;
	tm->bHour = hour;
	tm->bMinute = minute;
	tm->bMonth = month;
	tm->bSecond = second;
	tm->dwYear = year;
}

void TimeMark::set(TimeMark* tm, LPCTSTR strDate, LPCTSTR strTime)
{
	int year, month, day, hour, minute, second;
	year = month = day = hour = minute = second = 0;
	TCHAR szMonth[20] = { _T('\0') };
	sscanf(strTime, _T("%d:%d:%d"), &hour, &minute, &second);
	sscanf(strDate, _T("%s %d,%d"), szMonth, &day, &year);
	CString str(szMonth);
	if( !str.CompareNoCase(_T("january")) ) month = 1;
	else if( !str.CompareNoCase(_T("february")) ) month = 2;
	else if( !str.CompareNoCase(_T("march")) ) month = 3;
	else if( !str.CompareNoCase(_T("april")) ) month = 4;
	else if( !str.CompareNoCase(_T("may")) ) month = 5;
	else if( !str.CompareNoCase(_T("june")) ) month = 6;
	else if( !str.CompareNoCase(_T("july")) ) month = 7;
	else if( !str.CompareNoCase(_T("august")) ) month = 8;
	else if( !str.CompareNoCase(_T("september")) ) month = 9;
	else if( !str.CompareNoCase(_T("october")) ) month = 10;
	else if( !str.CompareNoCase(_T("november")) ) month = 11;
	else if( !str.CompareNoCase(_T("december")) ) month = 12;
	tm->bDay = day;
	tm->bHour = hour;
	tm->bMinute = minute;
	tm->bMonth = month;
	tm->bSecond = second;
	tm->dwYear = year;
}

LONG TimeMark::toLong()
{
	return toLong(dwYear, bMonth, bDay, bHour, bMinute, bSecond);
}

LONG TimeMark::toLong(int year, int month, int day, int hour, int minute, int second)
{
	DWORD y = year - 1;
	LONG res = ( y > 2020 )
			   ? ((y <= 2002) ? 0L : 0x4F2F180L*(y-2002))
			   : 0x4F2F180L*(y-2002);
	return res + (0x28DE80L * (month - 1))
			   + (0x15180L * (day - 1))
			   + (3600L * (hour - 1))
			   + (60L * (minute - 1))
			   + second;
}

LPTSTR TimeMark::toString(LPTSTR p)
{
	_stprintf(p, _T("%.4d %.2d %.2d %.2d %.2d %.2d"), (int)dwYear, (int)bMonth, (int)bDay, (int)bHour, (int)bMinute, (int)bSecond);
	return p;
}

CString TimeMark::toString()
{
	CString p;
	p.Format(_T("%.4d %.2d %.2d %.2d %.2d %.2d"), (int)dwYear, (int)bMonth, (int)bDay, (int)bHour, (int)bMinute, (int)bSecond);
	return p;
}

int TimeMark::compare(TimeMark& tm2)
{
	if(toLong() > tm2.toLong()) return 1;
	if(toLong() < tm2.toLong()) return -1;
	return 0;
}

#ifdef _JNC_REC_
CString TimeMark::toJNCString()
{
	char str[32];
	char str1[8];
	switch(bMonth)
	{
		case  1: strcpy(str1, "Jan"); break;
		case  2: strcpy(str1, "Feb"); break;
		case  3: strcpy(str1, "Mar"); break;
		case  4: strcpy(str1, "Apr"); break;
		case  5: strcpy(str1, "May"); break;
		case  6: strcpy(str1, "Jun"); break;
		case  7: strcpy(str1, "Jul"); break;
		case  8: strcpy(str1, "Aug"); break;
		case  9: strcpy(str1, "Sep"); break;
		case 10: strcpy(str1, "Oct"); break;
		case 11: strcpy(str1, "Nov"); break;
		case 12: strcpy(str1, "Dec"); break;
		default: strcpy(str1, "---"); break;
	}
	sprintf(str, "%s%.2d%.4d%.2d%.2d%.2d", str1,
			(int)bDay, (int)dwYear, (int)bHour, (int)bMinute, (int)bSecond);
	return str;
}

BOOL TimeMark::parseJNCString(CString& str)
{
	int i;
	i = _ttoi(str.Mid(3, 2));
	if((i <= 0) || (i > 31))
		return FALSE;
	bDay = i;
	i = _ttoi(str.Mid(5, 4));
	if((i < 2006) || (i > 2020))
		return FALSE;
	dwYear = i;
	i = _ttoi(str.Mid(9, 2));
	if((i < 0) || (i > 23))
		return FALSE;
	bHour = i;
	i = _ttoi(str.Mid(11, 2));
	if((i < 0) || (i > 59))
		return FALSE;
	bMinute = i;
	i = _ttoi(str.Mid(13, 2));
	if((i < 0) || (i > 59))
		return FALSE;
	bSecond = i;
	str = str.Left(3);
	if(str.CompareNoCase(_T("Jan")) == 0) { bMonth =  1; return TRUE; }
	if(str.CompareNoCase(_T("Feb")) == 0) { bMonth =  2; return TRUE; }
	if(str.CompareNoCase(_T("Mar")) == 0) { bMonth =  3; return TRUE; }
	if(str.CompareNoCase(_T("Apr")) == 0) { bMonth =  4; return TRUE; }
	if(str.CompareNoCase(_T("May")) == 0) { bMonth =  5; return TRUE; }
	if(str.CompareNoCase(_T("Jun")) == 0) { bMonth =  6; return TRUE; }
	if(str.CompareNoCase(_T("Jul")) == 0) { bMonth =  7; return TRUE; }
	if(str.CompareNoCase(_T("Aug")) == 0) { bMonth =  8; return TRUE; }
	if(str.CompareNoCase(_T("Sep")) == 0) { bMonth =  9; return TRUE; }
	if(str.CompareNoCase(_T("Oct")) == 0) { bMonth = 10; return TRUE; }
	if(str.CompareNoCase(_T("Nov")) == 0) { bMonth = 11; return TRUE; }
	if(str.CompareNoCase(_T("Dec")) == 0) { bMonth = 12; return TRUE; }
	return FALSE;
}
#endif //def _JNC_REC_
